Room = {}

function Room:New(roomId, playerNum, playerNumMax, isPlaying, players)
    return {
        roomId = roomId,
        playerNum = playerNum,
        playerNumMax = playerNumMax,
        isPlaying = isPlaying,
        players = players
    }
end

function Room:NewDefault(roomId)
    return Room:New(roomId, 0, 2, false, {})
end

function Room:AddPlayer(room, player)
    if room.playerNum < room.playerNumMax then
        -- 可以加入
        room.playerNum = room.playerNum + 1
        room.players[room.playerNum] = player
        return true
    else
        -- 无法加入
        return false
    end
end

function Room:QuitByName(room, name)
    for i, player in ipairs(room.players) do
        if player.username == name then
            table.remove(room.players, i)
            room.playerNum = room.playerNum - 1
            return true
        end
    end
    return false
end

function Room:ReadyByName(room, name)
    for i, player in ipairs(room.players) do
        if player.username == name then
            player.isReady = true
            room.players[i].isReady = true
            return true
        end
    end
    return false
end

function Room:UnReadyByName(room, name)
    for i, player in ipairs(room.players) do
        if player.username == name then
            player.isReady = false
            return true
        end
    end
    return false
end

function Room:IsReady(room)
    local notReady = false
    for i, player in ipairs(room.players) do
        if not player.isReady then
            notReady = true
        end
    end
    return not notReady
end

function Room:Start(room)
    room.isPlaying = true
end

function Room:GameOver(room)
    room.isPlaying = false
    for i, player in ipairs(room.players) do
        player.isReady = false
    end
end

return Room
